package com.kkd.kkdapp.view;

import android.R.bool;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.ViewParent;
import android.widget.HorizontalScrollView;

public class KKDHorizontalScrollView extends HorizontalScrollView {
	public static boolean FLAG_CANPULLDOWN = true;

	public KKDHorizontalScrollView(Context context, AttributeSet attrs,
			int defStyle) {
		super(context, attrs, defStyle);
	}

	public KKDHorizontalScrollView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public KKDHorizontalScrollView(Context context) {
		super(context);
	}

	float dertaY = 0;
	float lastY = 0;
	float dertaX = 0;
	float lastX = 0;

	/*
	 * (non-Javadoc) 避免父控件拦截touch事件
	 */
	@Override
	public boolean dispatchTouchEvent(MotionEvent ev) {

		switch (ev.getAction()) {
		case MotionEvent.ACTION_DOWN:
			dertaY = 0;
			lastY = ev.getY();
			dertaX = 0;
			lastX = ev.getX();
			break;
		case MotionEvent.ACTION_MOVE:
			dertaY = ev.getY() - lastY;
			dertaX = ev.getX() - lastX;
			lastX = ev.getX();
			lastY = ev.getY();
			if (Math.abs(dertaX) > Math.abs(dertaY)) {
				ViewParent parent = getParent();
				while (parent != null) {
					parent.requestDisallowInterceptTouchEvent(true);
					parent = parent.getParent();
				}
				FLAG_CANPULLDOWN = false;
			} else {
				FLAG_CANPULLDOWN = true;
			}
			break;
		case MotionEvent.ACTION_UP:
			FLAG_CANPULLDOWN = true;
			break;
		default:
			break;
		}
		return super.dispatchTouchEvent(ev);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * android.support.v4.view.ViewPager#onTouchEvent(android.view.MotionEvent)
	 */
	@Override
	public boolean onTouchEvent(MotionEvent arg0) {
		try {
			super.onTouchEvent(arg0);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return true;
	}
}
